from django.db import models
from utils.base_model import BaseModel
from django.contrib.auth.models import AbstractUser
from itsdangerous import TimedJSONWebSignatureSerializer as TJWSS
from electronice import settings
from mdeditor.fields import MDTextField


class Status(BaseModel):
    """
    状态
    """
    status = models.CharField(max_length=20, unique=True, verbose_name="状态")

    class Meta:
        db_table = "status"
        verbose_name = "状态"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.status


class Role(BaseModel):
    """
    角色
    """
    role = models.CharField(max_length=20, unique=True, verbose_name="角色")

    class Meta:
        db_table = "role"
        verbose_name = "角色"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.role


class AreaInfo(BaseModel):
    """
    地域信息:省市级联
    """
    area_name = models.CharField(max_length=20, verbose_name="地域名")
    pid = models.ForeignKey('self', on_delete=models.DO_NOTHING, blank=True, null=True, verbose_name="地域级联")

    class Meta:
        db_table = "areainfo"
        verbose_name = "省市级联"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.area_name


class Vip(BaseModel):
    """
    会员等级
    """
    vip = models.CharField(max_length=10, unique=True, verbose_name="会员")

    class Meta:
        db_table = "vip"
        verbose_name = "会员"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.vip


class Sex(BaseModel):
    """
    性别
    """
    sex = models.CharField(max_length=10, unique=True, verbose_name="性别")

    class Meta:
        db_table = "sex"
        verbose_name = "性别"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.sex


class User(AbstractUser, BaseModel):
    """
    用户表, 继承与django用户表
    """
    status = models.ForeignKey(Status, blank=True, null=True, on_delete=models.CASCADE, verbose_name="用户状态")
    phone = models.CharField(max_length=11, blank=True, null=True, unique=True, verbose_name="手机号")

    class Meta:
        db_table = "users"
        verbose_name = "用户表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username

    def generate_activate_tocken(self):
        '''生成用户签名字符串
        tjwss = TJWSS(秘钥, 有效期(单位秒))
        Django项目中setting中自带了一个秘钥直接使用,其他地方 可以先生成一个秘钥,再使用
        '''
        tjwss = TJWSS(settings.SECRET_KEY, 3600)
        info = {'confirm': self.id}
        tocken = tjwss.dumps(info)
        return tocken


class UserInfo(BaseModel):
    """
    用户详细信息
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="关联用户表")
    sex = models.ForeignKey(Sex, on_delete=models.CASCADE, blank=True, null=True, verbose_name="性别")
    head_photo = models.ImageField(verbose_name="头像")
    integral = models.IntegerField(default=0, verbose_name="积分")
    sign = models.IntegerField(default=0, verbose_name="签到天数")
    vip = models.ForeignKey(Vip, on_delete=models.CASCADE, verbose_name="会员等级")

    class Meta:
        db_table = "userinfo"
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        
        return self.user.username


class Address(BaseModel):
    """
    用户收货地址
    """
    status_chioces = (
        (0, '其他地址'),
        (1, '默认地址'),
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="关联用户表")
    people = models.CharField(max_length=20, verbose_name="收件人")
    phone = models.CharField(max_length=11, verbose_name="手机号")
    province = models.ForeignKey(AreaInfo, on_delete=models.DO_NOTHING, related_name="province", verbose_name="省")
    town = models.ForeignKey(AreaInfo, on_delete=models.DO_NOTHING, related_name="town", verbose_name="市")
    district = models.ForeignKey(AreaInfo, on_delete=models.DO_NOTHING, related_name="district", verbose_name="县")
    address = models.CharField(max_length=30, verbose_name="具体地址")
    status = models.SmallIntegerField(default=1, choices=status_chioces, verbose_name='地址状态')

    class Meta:
        db_table = "address"
        verbose_name = "用户地址"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.address


class GoodsType(BaseModel):
    """
    商品类型:省市级联
    """
    name = models.CharField(max_length=30, verbose_name="商品类型名")
    gt = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE, verbose_name="商品级联")

    class Meta:
        db_table = "goods_type"
        verbose_name = "商品类型"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Size(BaseModel):
    """
    型号:尺寸
    """
    size = models.CharField(max_length=10, unique=True, verbose_name="尺寸")

    class Meta:
        db_table = "size"
        verbose_name = "尺寸"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.size


class Color(BaseModel):
    """
    颜色
    """
    color = models.CharField(max_length=10, unique=True, verbose_name="型号")

    class Meta:
        db_table = "color"
        verbose_name = "颜色"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.color


class VendorType(BaseModel):
    """
    供应商类型
    """
    vendor_type = models.CharField(max_length=20, unique=True, verbose_name="供应商类型")

    class Meta:
        db_table = "vendor_type"
        verbose_name = "供应商类型"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.vendor_type


class Vendor(BaseModel):
    """
    供应商
    """
    name = models.CharField(max_length=20, verbose_name="供应商")
    vendor = models.ForeignKey(VendorType, on_delete=models.CASCADE, verbose_name="供应商类型")

    class Meta:
        db_table = "vendor"
        verbose_name = "供应商"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(BaseModel):
    """
    商品表
    """
    status_chioces = (
        (0, '下架'),
        (1, '上架'),
    )
    name = models.CharField(max_length=100, verbose_name="商品名")
    image = models.ImageField(verbose_name="商品图片")
    status = models.SmallIntegerField(default=1, choices=status_chioces, verbose_name='商品状态')
    inventory = models.IntegerField(verbose_name="库存")
    size = models.ForeignKey(Size, blank=True, null=True, on_delete=models.CASCADE, verbose_name='尺寸')
    color = models.ForeignKey(Color, blank=True, null=True, on_delete=models.CASCADE, verbose_name='颜色')
    code = models.CharField(max_length=200, unique=True, verbose_name='编号')
    price = models.FloatField(verbose_name='价格')
    details = MDTextField(blank=True, null=True, verbose_name='商品详情')
    goodstype = models.ForeignKey(GoodsType, on_delete=models.CASCADE, verbose_name='商品类型')

    class Meta:
        db_table = "goods"
        verbose_name = "商品表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class WheelGoods(models.Model):
    """
    轮播商品表
    """
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')
    image = models.ImageField(verbose_name='图片')

    class Meta:
        db_table = "wheelgoods"
        verbose_name = "轮播商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class Estimate(BaseModel):
    """
    评价:用户反馈
    """
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='关联商品')
    estimate = models.TextField(blank=True, null=True, max_length=225, verbose_name='用户评价')
    photo = models.ImageField(blank=True, null=True, verbose_name='晒图')

    class Meta:
        db_table = "estimate"
        verbose_name = "用户反馈"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.estimate


class Shippingcart(BaseModel):
    """
    购物车
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='关联用户')
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='关联商品')
    num = models.IntegerField(default=1)

    class Meta:
        db_table = "shippingcart"
        verbose_name = "购物车"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class Collect(BaseModel):
    """
    收藏夹
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='关联用户')
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='关联商品')

    class Meta:
        db_table = "collect"
        verbose_name = "收藏夹"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class Order(BaseModel):
    """
    订单详情
    """
    status_chioces = (
        (0, '待支付'),
        (1, '待发货'),
        (2, '待收货'),
        (3, '已完成'),
    )
    code = models.CharField(max_length=50, unique=True, verbose_name='订单编号')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='购买用户')
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='订单商品')
    num = models.IntegerField(default=1, verbose_name='商品数量')
    status = models.SmallIntegerField(default=1, choices=status_chioces, verbose_name='订单状态')
    address = models.ForeignKey(Address, on_delete=models.CASCADE, verbose_name='收货地址')

    class Meta:
        db_table = "order"
        verbose_name = "订单"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.code
